﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;
using System.ComponentModel;

namespace BDSim
{
    /// <summary>
    /// MessageReportView.xaml 的交互逻辑
    /// </summary>
    public partial class MessageReportView : UserControl,INotifyPropertyChanged
    {
        public MessageReportView()
        {
            InitializeComponent();  
        }
        #region 成员与属性
        /// <summary>
        /// INotifyPropertyChanged接口实现
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 定时器对象
        /// </summary>
        Timer timer = new Timer();

        bool _CanRunTimer;
        /// <summary>
        /// 标识是否可以运行定时器
        /// </summary>
        public bool CanRunTimer
        {
            get { return _CanRunTimer; }
            set { 
                _CanRunTimer = value; 
                timer.Enabled = !value;
                PropertyChanged(this, new PropertyChangedEventArgs("CanRunTimer"));
            }
        }
        #endregion

        #region 窗口启动加载事件
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            timer.Interval = 500;
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Enabled = true;
        }
        #endregion

        #region 定时器定期执行的事件
        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                //listView1选择最新项
                listView1.SelectedIndex = listView1.Items.Count - 1;
                //listView1滚动到最新项
                listView1.ScrollIntoView(listView1.SelectedItem);
            }));
        }
        #endregion

        #region 清除当前通知内容按钮事件
        private void btnClearCurrent_Click(object sender, RoutedEventArgs e)
        {
            //listView1.Items.Clear();
            //此处利用了引用类型的特点，对DataContent的内容进行修改
            MainWindowViewModel dataContent = (MainWindowViewModel)this.DataContext;
            dataContent.SysReportList.Clear();
        }
        #endregion
    }
}
